package com.ruoyi.rescue.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.rescue.api.ReUserNameService;
import com.ruoyi.rescue.api.domain.CheUser;
import com.ruoyi.rescue.domain.CheCar;
import com.ruoyi.rescue.service.ICheCarService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rescue.domain.CheRescue;
import com.ruoyi.rescue.service.ICheRescueService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 救援任务Controller
 * 
 * @author ruoyi
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/rescue")
@CrossOrigin
//---------------
public class CheRescueController extends BaseController
{
    @Autowired
    private ICheRescueService cheRescueService;
    @Autowired
    private ReUserNameService reUserNameService;

    @Autowired
    private ICheCarService cheCarService;

    /**
     * 查询救援任务列表
     */
    //--------------------
//    @RequiresPermissions("rescue:rescue:list")
    @GetMapping("/list")
    public TableDataInfo list(CheRescue cheRescue)
    {
        startPage();
        List<CheRescue> list = cheRescueService.selectCheRescueList(cheRescue);
        for(CheRescue rescue : list){
            //获取救助人
            AjaxResult info = reUserNameService.getInfo(rescue.getUserId(), SecurityConstants.INNER);
            LinkedHashMap data  = (LinkedHashMap) info.get("data");
            CheUser cheUser = BeanUtil.mapToBean(data, CheUser.class, true);
            rescue.setRealName(cheUser.getRealName());
//            System.out.println(cheUser.getRealName());
            //获取车牌
            CheCar cheCar = cheCarService.selectCheCarById(rescue.getCarId());
            rescue.setCarNumber(cheCar.getCarNumber());
            //救援类型名
            String typaName = cheRescueService.selectType(rescue.getId());
            rescue.setTypeName(typaName);

        }
        return getDataTable(list);
    }

    /**
     * 导出救援任务列表
     */
    @RequiresPermissions("rescue:rescue:export")
    @Log(title = "救援任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheRescue cheRescue)
    {
        List<CheRescue> list = cheRescueService.selectCheRescueList(cheRescue);
        ExcelUtil<CheRescue> util = new ExcelUtil<CheRescue>(CheRescue.class);
        util.exportExcel(response, list, "救援任务数据");
    }

    /**
     * 获取救援任务详细信息
     */
    @RequiresPermissions("rescue:rescue:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheRescueService.selectCheRescueById(id));
    }

    /**
     * 新增救援任务
     */
    @RequiresPermissions("rescue:rescue:add")
    @Log(title = "救援任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheRescue cheRescue)
    {
        return toAjax(cheRescueService.insertCheRescue(cheRescue));
    }

    /**
     * 修改救援任务
     */
    @RequiresPermissions("rescue:rescue:edit")
    @Log(title = "救援任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheRescue cheRescue)
    {
        return toAjax(cheRescueService.updateCheRescue(cheRescue));
    }

    /**
     * 删除救援任务
     */
    @RequiresPermissions("rescue:rescue:remove")
    @Log(title = "救援任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheRescueService.deleteCheRescueByIds(ids));
    }
}
